package com.kly.chatter.api.application.service.impl;

import com.alibaba.fastjson.JSON;
import com.kly.chatter.api.application.bo.MessageExtraBo;
import com.kly.chatter.api.application.service.AsyncSendMsgService;
import com.kly.sc.api.application.consts.Constants;
import com.kly.sc.api.integration.im.RCClient;
import com.kly.user.chatterService.ChatService;
import com.kly.user.dto.AddMessageReq;
import com.kly.user.dto.chatter.request.ConversationReq;
import com.kly.user.enums.AppEnum;
import com.kly.user.service.OtherService;
import io.rong.messages.InfoNtfMessage;
import io.rong.messages.TxtMessage;
import io.rong.models.message.PrivateMessage;
import io.rong.models.response.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class AsyncSendMsgServiceImpl implements AsyncSendMsgService {

    @Resource
    RCClient rcClient;
    @Resource
    OtherService otherService;

    @Resource
    ChatService chatService;

    @Override
    @Async("taskExecutor")
    public void sendMsg(String currentUserId, String conversationType, String targetId, String objectName, String content, String pushContent, String chatterUserCode, int type, List<String> replaceWord) {
        ResponseResult responseResult = null;
        if (Constants.CONVERSATION_TYPE_PRIVATE.equals(conversationType)) {
            PrivateMessage privateMessage = new PrivateMessage()
                    .setSenderId(currentUserId)
                    // 是否向发件人客户端同步已发消息。1 表示同步 0，即不同步
                    .setIsIncludeSender(1)
                    .setTargetId(new String[]{targetId})
                    .setObjectName(objectName)
                    .setContent(new TxtMessage(content, ""))
                    .setPushContent(pushContent);
            responseResult = rcClient.sendPrivateMessage(privateMessage);
            if (Objects.nonNull(responseResult) && responseResult.getCode() == 200) {
                final AddMessageReq addMessageReq = AddMessageReq.builder().msgUID(responseResult.getRequestId()).fromUserId(currentUserId)
                        .toUserId(targetId).objectName("RC:TxtMsg").content(content).source("Server").groupUserIds(chatterUserCode).build();
                otherService.addMessage(addMessageReq);
            }
        } else {
            MessageExtraBo messageExtraBo = new MessageExtraBo();
            messageExtraBo.setType(type);
            messageExtraBo.setReplaceWord(replaceWord);
            PrivateMessage privateMessage = new PrivateMessage()
                    .setSenderId(currentUserId)
                    // 是否向发件人客户端同步已发消息。1 表示同步 0，即不同步
                    .setIsIncludeSender(0)
                    .setTargetId(new String[]{targetId})
                    .setObjectName(objectName)
                    .setContent(new InfoNtfMessage(content, JSON.toJSONString(messageExtraBo)))
                    .setPushContent(pushContent).setVerifyBlacklist(0).setIsPersisted(0).setDisablePush(false);
            responseResult = rcClient.sendPrivateMessage(privateMessage);
//            if (Objects.nonNull(responseResult) && responseResult.getCode() == 200) {
//                log.info("====sendMsg, ");
//                final ConversationReq conversationReq = ConversationReq.builder().latestMsg(pushContent).latestTime(new Date()).source(AppEnum.MEBOO.getAppCode()).type(1).fromUserCode(currentUserId).toUserCode(targetId).chatterUserCode(chatterUserCode).latestMsg(content).latestTime(new Date()).build();
//                chatService.storageConversation(conversationReq);
//                // 是新mechat会话列表中的最新消息
//            }
        }
    }
}
